package com.mmy.acassistant.model.main.ui.activity

import android.support.design.widget.BottomNavigationView
import android.view.Menu
import android.view.MenuItem
import cn.pedant.SweetAlert.SweetAlertDialog
import com.mmy.acassistant.R
import com.mmy.acassistant.model.f2.ui.fragment.F2Fragment
import com.mmy.acassistant.model.f3.ui.fragment.F3Fragment
import com.mmy.acassistant.model.main.component.DaggerMainComponent
import com.mmy.acassistant.model.main.module.MainModule
import com.mmy.acassistant.model.main.presenter.MainPresenter
import com.mmy.acassistant.model.main.ui.fragment.HomeFragment
import com.mmy.acassistant.model.main.view.MainView
import com.mmy.frame.AppComponent
import com.mmy.frame.base.view.BaseActivity
import kotlinx.android.synthetic.main.activity_main.*

/**
* @file       MainActivity.kt
* @brief      描述
* @author     lucas
* @date       2018/1/15 0015
* @version
* @par        Copyright (c):
* @par History:
*             version: zsr, 2017-09-23
*/
class MainActivity : BaseActivity<MainPresenter>(), MainView {

    override fun showCityName(city: String) {
    }

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                return@OnNavigationItemSelectedListener true
            }
        }
        showFragmentByPosition(item.order)
        false
    }

    private fun showFragmentByPosition(order: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        val showFragment = supportFragmentManager.findFragmentByTag(mFragmentTags[order])
        if (showFragment == null) {
            transaction.add(R.id.fragment_content, mFragments[order])
        } else {
            transaction.show(showFragment)
        }
        mFragmentTags.forEach {
            if (!it.equals(mFragmentTags[order])) {
                val fragment = supportFragmentManager.findFragmentByTag(it)
                if (fragment != null)
                    transaction.hide(fragment)
            }
        }
    }

    override fun getLayoutID(): Int = R.layout.activity_main

    override fun setupDagger(appComponent: AppComponent) {
        DaggerMainComponent
                .builder()
                .appComponent(appComponent)
                .mainModule(MainModule(this))
                .build().inject(this)
    }

    val mFragmentTags = arrayOf("home", "f2", "f3")
    val mFragments = arrayOf(HomeFragment(), F2Fragment(), F3Fragment())
    var updataVersionAlert: SweetAlertDialog? = null

    override fun initView() {
        setSupportActionBar(toolbar)
        supportActionBar?.title = ""

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        //add fragment
        supportFragmentManager.beginTransaction()
                .add(R.id.fragment_content, mFragments[0], mFragmentTags[0])
                .commit()

        updataVersionAlert = SweetAlertDialog(this, SweetAlertDialog.NORMAL_TYPE)
                .setTitleText("发现新版本")
                .setContentText("是否立即更新？")
                .setConfirmText("立即更新")
    }

    override fun initData() {
        mIPresenter.checkVersion()
    }

    override fun initEvent() {
    }

    override fun onVersionChange() {
        updataVersionAlert?.show()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_settings -> return true
            else -> return super.onOptionsItemSelected(item)
        }
    }

}
